import { Module } from 'vuex'
import { categorieParamsModuleType, GlobalDataProps, attrValsProps, categoriesObjProps } from '@/store/storeType'
import { getFn, putFn, postFn } from '@/store/store_actions_axiosFn'
import { deleteFun } from '@/utils/axiosFunc'

const paramsModule: Module<categorieParamsModuleType, GlobalDataProps> = {
  state: () => ({
    categoriesList: [], // 分类参数选项列表
    categoriesObj: {// 动态参数数据和静态属性数据
      many: [],
      only: []
    }
  }),
  mutations: {
    getCategoriesList (state, data) {
      if (data.status === 200) {
        state.categoriesList = data.data.data
      }
    },
    getIdCategories (state, data) {
      if (data.status === 200) {
        if (data.data.data.length !==0 && data.data.data[0].attr_sel === 'many') {
          data.data.data.forEach((item: attrValsProps, index: number) => {
            if (state.categoriesObj.many[index]?.attr_id && item.attr_id !== state.categoriesObj.many[index]?.attr_id) {
              item.attr_vals = (item.attr_vals as string).split(',')
              state.categoriesObj.many.push(item)
            } else if (!state.categoriesObj.many[index]) {
              item.attr_vals = (item.attr_vals as string).split(',')
              state.categoriesObj.many.push(item)
            }
          })
        } else {
          data.data.data.forEach((item: attrValsProps, index: number) => {
            if (state.categoriesObj.only[index]?.attr_id && item.attr_id !== state.categoriesObj.only[index]?.attr_id) {
              item.attr_vals = (item.attr_vals as string).split(',')
              state.categoriesObj.only.push(item)
            } else if (!state.categoriesObj.only[index]?.attr_id) {
              item.attr_vals = (item.attr_vals as string).split(',')
              state.categoriesObj.only.push(item)
            }
          })
        }
      } else {
        return
      }
    },
    putIdCategories (state, data) {
      if (data.data.meta.status !== 200) return
      const { data: res } = data.data
      const { attr_sel, attr_id } = data.data.data
      state.categoriesObj[attr_sel as 'many' | 'only'] = state.categoriesObj[attr_sel as 'many' | 'only'].map(item => {
        if (item.attr_id === attr_id) {
          item = res
        }
        return item
      })
    },
    postAddAttributes (state, data) {
      if (data.status !== 200) return
      const { data: res } = data.data
      if (state.categoriesObj[res.attr_sel as 'many' | 'only'][0]) {
        state.categoriesObj[res.attr_sel as 'many' | 'only'].push(res)
      }
    },
    categorieDelete (state, data) {
      const { attr_id, res, attr_sel } = data
      if (res.status !== 200) return
      const newArr = state.categoriesObj[attr_sel as 'many' | 'only'].filter(item => item.attr_id !== attr_id)
      state.categoriesObj[attr_sel as 'many' | 'only'] = newArr
    },
  },
  actions: {
    getCategoriesList ({ commit }, parems = {}) {
      return getFn('categories', 'getCategoriesList', commit, parems)
    },
    getIdCategories ({ commit }, parmas) {
      return getFn(`categories/${parmas.cid}/attributes`, 'getIdCategories', commit, { sel: parmas.sel })
    },
    putIdCategories ({ commit }, putData) {
      return putFn(`categories/${putData.cateId}/attributes/${putData.attr_id}`, putData.data,'putIdCategories', commit)
    },
    postAddAttributes ({ commit }, postData) {
      return postFn(`categories/${postData.cat_id}/attributes`, postData.data, 'postAddAttributes', commit)
    },
    async categorieDelete ({ commit }, delData) {
      const res = await deleteFun(`categories/${delData.cat_id}/attributes/${delData.attr_id}`)
      commit('categorieDelete', { attr_id: delData.attr_id, res, attr_sel: delData.attr_sel })
      return res
    },
  },
  getters: {
    CategorieToId: state => (id: number, activeName: 'many' | 'only') => (state.categoriesObj as categoriesObjProps)[activeName].filter((item: any) => item.cat_id === id)
  },
}

export default paramsModule